# -*- coding: utf-8 -*-
import re
from w.utils.general import Object
from keyword import iskeyword

def StaticInit(unique=True,**vargs):
    if unique:
        postfix = hash(str(datetime.now()))
    else:
        postfix = hash(tuple(sorted(vargs.items())))
    return type('type%s'%postfix, (object,), vargs)

class DynamicInit(Object):
    def __init__(self, **vargs):
        for n,v in vargs.items():
            setattr(self,n,v)

class FilteredDynamicInit(Object):
    _argNameRE = re.compile('^_')
    _collector = Object()
    autoCapitalizedKeyword = True

    def __init__(self, **vargs):
        for n,v in vargs.items():
            nl = self._argNameRE.split(n,1)
            if len(nl) > 1:
                n = nl[1]
                if iskeyword(n) and self.autoCapitalizedKeyword:
                    n = n.capitalize()
                setattr(self._collector, n, v)
            else:
                setattr(self, n, v)

class DynamicAttribute(Object):
    def __getattribute__(self, name):
        if name != '__dict__' and name not in self.__dict__:
            setattr(self, name, None)
        return object.__getattribute__(self, name)

class DynamicObject(DynamicInit, DynamicAttribute):
    pass

class FilteredDynamicObject(FilteredDynamicInit, DynamicAttribute):
    pass

DObject = DynamicObject
FDObject = FilteredDynamicObject
